<!-- 分类页面 -->
<template>
  <view>
    <!-- 搜索栏区域 -->
    <my_search></my_search>
    <!-- 左侧滑动区域 -->
    <view class="scroll_view_box">
      <scroll-view class="scroll_view_left" scroll-y :style="{height:windowHeight + 'px'}" >
        <!-- 一级分类 -->
        <block v-for="(item,index) in cateList" :key="index">
          <view :class="['scroll_item',index === active?'active':'']"
          @click="clickClass(index)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧滑动区 -->
      <scroll-view 
      class="scrll_view_right" 
      scroll-y 
      :style="{height:windowHeight + 'px'}"
      :scrollTop='scrollTop'>
        <view v-for="(item,index) in cateListTow">
          <!-- 二级分类标题-->
          <view class="cate_tow" v-if="item.children">/ {{item.cat_name}} /</view>
         <!--二级分类下的三级分类列表  -->
          <view class="cate_three">
            <view 
              v-for="(itm,ind) in item.children" 
              class="cate_three_item" 
              @click="cateJump(itm)">
                <image :src="itm.cat_icon"></image>
                <text>{{itm.cat_name}}</text>
            </view>
          </view>
       </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 屏幕可使用区域高度
        windowHeight:0,
        // 左侧列表数据 
        cateList:[],
        // 动态添加类名所需的标识
        active: 0,
        // 二级分类
        cateListTow:[],
        scrollTop:0
      };
    },
    onLoad() {
      // uni.getSystemInfoSync：获取设备信息的api
      this.windowHeight = uni.getSystemInfoSync().windowHeight - 50
      this.getCateList()
    },
    methods:{
      async getCateList() {
      const {data} = await uni.$http.get('/api/public/v1/categories') 
      if(data.meta.status !== 200) return uni.$showMsg()
      this.cateList = data.message
      this.cateListTow = data.message[0].children
      },
      clickClass(index){
        this.active = index
        // 重新给二级分类数组赋值
        this.cateListTow = this.cateList[index].children
        // 解决切换一级分类，二级分类显示不置顶的问题
        this.scrollTop = this.scrollTop === 0 ? 1 : 0  
      },
      // 跳转商品列表页面
      cateJump(itm) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + itm.cat_id
        })
      }
    }
  }
</script>

<style lang="scss">
.scroll_view_box {
  display: flex;
  
  .scroll_view_left {
    width: 120px;
    
    .scroll_item {
      background-color: #f7f7f7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      &.active {
        background-color: #ffff;
        position: relative;
        &::before {
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #FF0000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%)
        }
      }
    }
  }
  .scrll_view_right {
    width: 100%;
    
    .cate_tow {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }
    .cate_three{
      display: flex;
      flex-wrap: wrap;
      .cate_three_item {
        width: 33.333%;
        margin-bottom: 10px;
        display: flex;
        flex-direction:column;
        justify-content: center;
        align-items: center;
        
        image {
          width: 60px;
          height: 60px;
        }
        text {
          font-size: 12px;
        }
      }
    }   
  }
}
</style>
